<?php


namespace app\http\middleware;

use app\common\controller\CommonService;
use think\facade\Request;

class PartnerCheck
{
    public function handle($request, \Closure $next)
    {
        $agentToken = Request::header('partnerAdmin');
        $jwtDecode = (array)CommonService::jwtDecode($agentToken);
        if (isset($jwtDecode['id']) && $jwtDecode['id'] > 0) {
            //判断单点登录
            if ($agentToken !== cache('partnerAdmin' . $jwtDecode['id'])) {
                return CommonService::error('账号已在其他设备登录！', '', -1);
            }
            $request->partnerId = $jwtDecode['id'];
            return $next($request);
        }
        return CommonService::error('登录失效！', '', -1);
    }
}